﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;

namespace BelleApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //Now display the filename in the textbox.
                textBox1.Text = ofd.FileName;
                XmlDocument doc = new XmlDocument();//create new doc
                doc.Load(ofd.FileName); //load from file directory as seen in textbox
                XmlNodeList rfqList = doc.GetElementsByTagName("Item");
                StringBuilder sb = new StringBuilder();

                foreach (XmlNode node in rfqList)
                {
                    if (node.Attributes.Count > 0)
                    {
                        sb.Append("Item ID= ").Append(node.Attributes[0].Value).Append(Environment.NewLine);
                    }
                    if (node.HasChildNodes)
                    {
                        XmlElement childNode = (XmlElement)node.FirstChild;
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            switch (childNode.Name)
                            {
                                case "Quantities":
                                    sb.Append("Quantities= ");
                                    break;

                                case "UnitPrice":
                                    sb.Append("UnitPrice= ");
                                    break;
                            }
                            sb.Append((childNode.InnerText) + Environment.NewLine);
                            childNode = (XmlElement)childNode.NextSibling;
                        }
                        sb.Append(Environment.NewLine);

                    }


                }
                //textBox1.Clear();
                textBox2.Text += sb;
            }
            else 
            {


                MessageBox.Show("Please Browse Your Files!");
            
            }
            
            
        }

        private void btnStore_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {

                MessageBox.Show("Please Browse your file!");

            }
            else
            {
                SqlConnection conn = new SqlConnection("server=localhost;initial catalog=Supplier2Database;integrated security=yes");
                string a = "insert into RFQDetail(RFQID,ItemID,Quantity,AskingPrice)"
                    + "values (@RFQID,@ItemId,@Quantity,@AskingPrice)";
                string x = "insert into RFQ(RFQID,CustomerID,RFQDate,Instructions)"
                    + "values (@RFQID,@CustomerId,@RFQDate,@Instructions)";
                conn.Open();

                XmlDocument b = new XmlDocument();
                b.Load(textBox1.Text);
                XmlNodeList listleh = b.GetElementsByTagName("RFQ");
                int t = 0;
                foreach (XmlNode node in listleh)
                {

                    t = Convert.ToInt32(node.Attributes[0].Value);

                }
                XmlNodeList listleh1 = b.GetElementsByTagName("Item");
                foreach (XmlNode node in listleh1)
                {
                    SqlCommand cmd = new SqlCommand(a, conn);

                    cmd.Parameters.AddWithValue("@RFQID", t);
                    cmd.Parameters.AddWithValue("@ItemId", node.Attributes[0].Value);
                    cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(node.ChildNodes[0].InnerText));
                    cmd.Parameters.AddWithValue("@AskingPrice", Convert.ToDouble(node.ChildNodes[1].InnerText));

                    cmd.ExecuteNonQuery();

                }
                XmlNodeList listleh3 = b.GetElementsByTagName("BuyerID");
                int g = 0;
                foreach (XmlNode node in listleh3)
                {
                    //g = Convert.ToInt32(node.Attributes[0].Value);
                    g = Convert.ToInt32(node.InnerText);
                }

                XmlNodeList listleh2 = b.GetElementsByTagName("RFQ");
                foreach (XmlNode node in listleh2)
                {
                    SqlCommand cmd = new SqlCommand(x, conn);

                    cmd.Parameters.AddWithValue("@RFQID", Convert.ToInt32(node.Attributes[0].Value));
                    cmd.Parameters.AddWithValue("@RFQDate", node.ChildNodes[0].InnerText);
                    cmd.Parameters.AddWithValue("@CustomerId", g);
                    cmd.Parameters.AddWithValue("@Instructions", node.ChildNodes[2].InnerText);

                    cmd.ExecuteNonQuery();

                }
                MessageBox.Show("Xml File is Stored in the Database!");
                textBox1.Clear();
                textBox2.Clear();
            }
        }
        private void btnQuotation_Click(object sender, EventArgs e)
        {
            SearchRFQ rfq = new SearchRFQ();
            rfq.Show();
            this.Hide();
        }
    }
}
